package git.soulbgm.utils;

/**
 * 字符串工具类
 *
 * @author SoulBGM
 * @date 2021/03/31
 */
public class StringUtil {

    /**
     * 判断1个或多个字符串是否不为空
     * 判断条件 null 和 长度不为0
     *
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String... str) {
        for (String s : str) {
            if (s == null) {
                return false;
            }
            if (s.trim().length() == 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断1个或多个字符串是否不为空
     * 判断条件 null 和 长度不为0
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String... str) {
        for (String s : str) {
            if (s == null) {
                return true;
            }
            if (s.trim().length() == 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 给字符规定额定长度 如果不足额定长度向⬅左填充空格
     *
     * @param str       原始字符串
     * @param strLength 额定长度
     * @return 额定长度的字符串
     */
    public static String leftFillSpace(String str, int strLength) {
        return ratedLength(str, strLength, " ", 0);
    }

    /**
     * 给字符规定额定长度 如果不足额定长度向➡右填充空格
     *
     * @param str       原始字符串
     * @param strLength 额定长度
     * @return 额定长度的字符串
     */
    public static String rightFillSpace(String str, int strLength) {
        return ratedLength(str, strLength, " ", 1);
    }

    /**
     * 给字符规定额定长度
     *
     * @param str       原始字符串
     * @param strLength 额定长度
     * @param fillStr   如果不足额定长度补位的字符 长度最好为1 否则会出现超出额定长度的问题
     * @param direction 补位的方向 0⬅左 其它为➡右
     * @return 额定长度的字符串
     */
    public static String ratedLength(String str, int strLength, String fillStr, int direction) {
        int strLen = str.length();
        StringBuilder sb = null;
        while (strLen < strLength) {
            sb = new StringBuilder();
            if (direction == 0) {
                // ⬅左补位
                sb.append(fillStr).append(str);
            } else {
                // ➡右补位
                sb.append(str).append(fillStr);
            }
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }

    /**
     * 将一组字符才以指定的字符链接起来
     *
     * @param linkStr 字符串与字符串连接之间的字符
     * @param strs    需要连接的动态参数
     * @return 连接之后的字符串
     */
    public static String join(String linkStr, String... strs) {
        if (isNotEmpty(strs) && linkStr != null) {
            StringBuilder result = new StringBuilder();
            for (String temp : strs) {
                result.append(temp);
                result.append(linkStr);
            }
            if (result.length() > linkStr.length()) {
                result.delete(result.length() - linkStr.length(), result.length());
            }
            return result.toString();
        }
        return null;
    }

    /**
     * 使用站位符拼接字符串
     * 格式：sdsdf{}jsk{}sss{}
     *
     * @param format   有占位符的字符串
     * @param argArray 占位符上具体的值
     * @return 拼接之后的字符串
     */
    public static String arrayFormat(String format, Object... argArray) {
        if (format == null || argArray == null) {
            return "";
        }
        StringBuilder sbuf = new StringBuilder(format.length() + 50);
        int index = 0;
        for (int i = 0; i < argArray.length; i++) {
            int j = format.indexOf("{}", index);
            if (j == -1) {
                sbuf.append(format, index, format.length());
                ArrayFormatCommon.deeplyAppendParameter(sbuf, argArray[i]);
                return sbuf.toString();
            } else {
                sbuf.append(format, index, j);
                ArrayFormatCommon.deeplyAppendParameter(sbuf, argArray[i]);
                index = j + 2;
            }
        }
        sbuf.append(format, index, format.length());
        return sbuf.toString();
    }

    static class ArrayFormatCommon {
        private static void deeplyAppendParameter(StringBuilder sbuf, Object o) {
            if (o == null) {
                sbuf.append("null");
            } else {
                if (!o.getClass().isArray()) {
                    sbuf.append(o.toString());
                } else if (o instanceof boolean[]) {
                    booleanArrayAppend(sbuf, (boolean[]) o);
                } else if (o instanceof byte[]) {
                    byteArrayAppend(sbuf, (byte[]) o);
                } else if (o instanceof char[]) {
                    charArrayAppend(sbuf, (char[]) o);
                } else if (o instanceof short[]) {
                    shortArrayAppend(sbuf, (short[]) o);
                } else if (o instanceof int[]) {
                    intArrayAppend(sbuf, (int[]) o);
                } else if (o instanceof long[]) {
                    longArrayAppend(sbuf, (long[]) o);
                } else if (o instanceof float[]) {
                    floatArrayAppend(sbuf, (float[]) o);
                } else if (o instanceof double[]) {
                    doubleArrayAppend(sbuf, (double[]) o);
                } else if (o instanceof String[]) {
                    stringArrayAppend(sbuf, (String[]) o);
                }
            }
        }

        static boolean isEscapedDelimeter(String messagePattern, int delimeterStartIndex) {
            if (delimeterStartIndex == 0) {
                return false;
            } else {
                char potentialEscape = messagePattern.charAt(delimeterStartIndex - 1);
                return potentialEscape == '\\';
            }
        }

        private static void booleanArrayAppend(StringBuilder sbuf, boolean[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void byteArrayAppend(StringBuilder sbuf, byte[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void charArrayAppend(StringBuilder sbuf, char[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void shortArrayAppend(StringBuilder sbuf, short[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void intArrayAppend(StringBuilder sbuf, int[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void longArrayAppend(StringBuilder sbuf, long[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void floatArrayAppend(StringBuilder sbuf, float[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void doubleArrayAppend(StringBuilder sbuf, double[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

        private static void stringArrayAppend(StringBuilder sbuf, String[] a) {
            sbuf.append('[');
            int len = a.length;

            for (int i = 0; i < len; ++i) {
                sbuf.append(a[i]);
                if (i != len - 1) {
                    sbuf.append(", ");
                }
            }

            sbuf.append(']');
        }

    }

}
